package com.macro.mall.tiny.component.sms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 短信工具类
 *
 * @author yelong.xie
 * @date 2024/7/5 15:32
 */
@Component
public class SmsClient {

    private static final Logger log = LoggerFactory.getLogger(SmsClient.class);

    @Autowired
    private RestTemplate restTemplate;

    //todo 请替换为自己的短信模板
    public static final String template = "xxxx";
    public static final String appCode = "e4e8832c05ef448ab7d6201676fa5699770fb76f00b546879133d554acbe7d68";

    /**
     * Response: {"ReturnStatus":"Faild","Message":"短信号码不能为空","RemainPoint":0,"TaskID":0,"SuccessCounts":0}
     *
     * @param mobile
     * @param code
     */
    public void send(String mobile, String code) {
        String content = String.format(template, code);
        String url = "https://codesms.apistore.huaweicloud.com/chuangxin/yzmdxjk?content=" + content + "&mobile=" + mobile;
        HttpHeaders headers = new HttpHeaders();
        headers.set("X-Apig-AppCode", appCode);
        headers.set("Content-Type", "application/x-www-form-urlencoded");
        headers.set("Charset", "UTF-8");
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        log.info("com.macro.mall.portal.component.sms.SmsClient#send({},{}) ,result:{}", mobile, code, response.getBody());
    }



}
